//
//  VisitorView.swift
//  Weibo
//
//  Created by apple on 15/12/6.
//  Copyright © 2015年 idcast. All rights reserved.
//

import UIKit

///访客试图的代理协议
protocol  VisitorViewDelegate: NSObjectProtocol {

    //注册
    func visitorViewDidRegister()
    
    //登录
    func visitorViewDidLogin()
}
///访客试图 处理用户为登陆的界面信息

class VisitorView: UIView {
    //设置代理属性 
   weak var delegate: VisitorViewDelegate?
    //监听方法
    @objc private func clickLogin() {
    
        delegate?.visitorViewDidLogin()
    }
    @objc private func clickRegister() {
    delegate?.visitorViewDidRegister()
    
    }
   
    //设置试图信息
    func setuoInfo(imageName: String?, title: String) {
    
        messageLabel.text = title
        
        guard let imgName = imageName else {
            
            startAnim()
        
            return
        }
       
        iconView.image = UIImage(named: imgName)
        //隐藏小房子
        homeIconView.hidden = true
       
        
    }
    //开启首页转轮动画
    private func startAnim() {
    
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        anim.removedOnCompletion = false
        
        //添加到图层
        iconView.layer.addAnimation(anim, forKey: nil)
    }

  //initwithfram是使用纯代码开发使用的 是uiview的指定构造函数
   override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    
    }

   required init?(coder aDecoder: NSCoder) {
//       fatalError("init(coder:) has not been implemented")
    super.init(coder: aDecoder)
    setupUI()
    
   }
    //懒加载控件
    //图标
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //遮罩图像
    private lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //小房子
    private lazy var homeIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //字体
    private lazy var messageLabel: UILabel = {
    
        let label = UILabel()
        
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        //下面这两条同时设置才能正常显示
        label.numberOfLines = 0
        label.textAlignment = NSTextAlignment.Center
        
        return label
    }()
    //注册按钮
    private lazy var registerButton: UIButton = UIButton(title: "注册", backimageName: "common_button_white_disable",color: UIColor.orangeColor())
  
    
    //登录按钮
    private lazy var loginButton: UIButton = UIButton(title: "登录", backimageName: "common_button_white_disable", color: UIColor.orangeColor())
  

}


extension VisitorView {
    
    private func setupUI() {
        
        //添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        //设置布局
        //
        for v in subviews {
        
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        //1 图标
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        //小房子
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        //文字
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: homeIconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: homeIconView, attribute: .Bottom, multiplier: 1.0, constant: 30))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        //注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Left, relatedBy: .Equal, toItem: messageLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        //登录按钮
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Right, relatedBy: .Equal, toItem: messageLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        
        
        //遮罩图像
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[mask]-0-|", options: [], metrics: nil, views: ["mask": maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[mask]-(btnHeight)-[regButton]", options: [], metrics: ["btnHeight": -36], views: ["mask": maskIconView, "regButton": registerButton]))
        //设置背景颜色
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)
        
        //添加监听方法
        registerButton.addTarget(self, action: "clickRegister", forControlEvents: .TouchUpInside)
        loginButton.addTarget(self, action: "clickLogin", forControlEvents: .TouchUpInside)
    }
    
    
}
